﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLKHO.Class;

namespace QLKHO.Forms
{
    public partial class frmChiTietPhieuXuat : Form
    {
        private QLKHODataSet.CHITIETPHIEUXUATDataTable dtPhieuXuat = new QLKHODataSet.CHITIETPHIEUXUATDataTable();
        public frmChiTietPhieuXuat()
        {

            InitializeComponent();
            this.cHITIETPHIEUXUATTableAdapter.Fill(this.qLKHODataSet.CHITIETPHIEUXUAT);
            txtHangHoa.fcProc_ListView = "proc_load_hanghoatheokho '" + clsMain.strMSKho + "'";
            
            clsMain.flexConn.InitForms(this.Tag.ToString(), this, qLKHODataSet.CHITIETPHIEUXUAT);
            clsMain.flexConn.InitForms(this.Tag.ToString(), ref fcButton1);
            clsMain.flexConn.InitForms(this.Tag.ToString(), ref dataGridView1);
            clsMain.flexConn.KhoiTaoXMLTable(this.Tag.ToString(), qLKHODataSet.CHITIETPHIEUXUAT);
        }

        private void frmChiTietPhieuXuat_Load(object sender, EventArgs e)
        {
            txtMSPhieuXuat.fcText = clsMain.strMSPhieuXuatKho;
            clsMain.flexConn.GenerateCode(16);
            LoadData();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = 0;
            foreach (DataRow row in qLKHODataSet.CHITIETPHIEUXUAT)
            {
                if (row["MASO"].ToString() == dtPhieuXuat.Rows[e.RowIndex]["MASO"])
                {
                    break;
                }
                rowIndex++;
            }
            clsMain.flexConn.AddDataToControls(this.Tag.ToString(), rowIndex, qLKHODataSet.CHITIETPHIEUXUAT);
        }

        private void fcButton1_Click(object sender, EventArgs e)
        {
            if (fcButton1.intState == FlexControls.flexControls.STATE_DELETE)
            {
                int RowHandle = clsMain.flexConn.Forms_QLHS.fcForms[16].intRowHandle;
                string MaSo = qLKHODataSet.CHITIETPHIEUXUAT.Rows[RowHandle]["MASO"].ToString();
                string MSHANGHOA = qLKHODataSet.CHITIETPHIEUXUAT.Rows[RowHandle]["MSHANGHOA"].ToString();
                int SOLUONGCHUNGTU = Convert.ToInt32(qLKHODataSet.CHITIETPHIEUXUAT.Rows[RowHandle]["SOLUONGCHUNGTU"].ToString());
                int SOLUONGTHUC = Convert.ToInt32(qLKHODataSet.CHITIETPHIEUXUAT.Rows[RowHandle]["SOLUONGTHUC"].ToString());
                int DONGIA = Convert.ToInt32(qLKHODataSet.CHITIETPHIEUXUAT.Rows[RowHandle]["DONGIA"].ToString());
                cHITIETPHIEUXUATTableAdapter.Delete(MaSo, clsMain.strMSPhieuXuatKho, MSHANGHOA, SOLUONGCHUNGTU, SOLUONGTHUC, DONGIA);
                this.cHITIETPHIEUXUATTableAdapter.Fill(this.qLKHODataSet.CHITIETPHIEUXUAT);
            }
            else
                clsMain.flexConn.ButtonsClick(this.Tag.ToString(), fcButton1.intState);

            cHITIETPHIEUXUATTableAdapter.Update(qLKHODataSet.CHITIETPHIEUXUAT);
            txtMSPhieuXuat.fcText = clsMain.strMSPhieuXuatKho;
            LoadData();
        }
        private void LoadData()
        {
            try
            {
                dtPhieuXuat.Clear();
                foreach (DataRow row in qLKHODataSet.CHITIETPHIEUXUAT)
                {
                    if (row["MSPHIEUXUAT"].ToString() == clsMain.strMSPhieuXuatKho)
                    {
                        DataRow rowPhieuXUAT = dtPhieuXuat.NewRow();
                        for (int i = 0; i < qLKHODataSet.CHITIETPHIEUXUAT.Columns.Count; i++)
                        {
                            rowPhieuXUAT[i] = row[i];

                        }
                        dtPhieuXuat.Rows.Add(rowPhieuXUAT);
                    }
                }
                cHITIETPHIEUXUATBindingSource1.DataSource = dtPhieuXuat;

                clsMain.intThanhTien = 0;
                int j = 0;
                foreach (DataRow row in dtPhieuXuat.Rows)
                {
                    string ThanhTien = Convert.ToString(Convert.ToInt32(row["SOLUONGTHUC"].ToString()) * Convert.ToInt32(row["DONGIA"].ToString()));
                    dataGridView1.Rows[j].Cells["THANHTIEN"].Value = ThanhTien;
                    j++;
                    clsMain.intThanhTien += Convert.ToInt32(ThanhTien);
                }

                txtThanhTien.Text = clsMain.intThanhTien.ToString("c0");
            }
            catch (Exception ex)
            {
                MessageBox.Show("LoadData\n" + ex.ToString());
            }

        }
    }
}
